#include <stdio.h>

using namespace std;

const int INF = 999999;

int N, M;

int Len[10], Mask[10];
int min_diff = INF;

void Solve(int cur) {  // 处理第cur个木棍，把它分配给M段中的一个。
  if (cur == N) {
    int len[10] = {};
    for (int i = 0; i < N; ++i) {
      len[Mask[i]] += Len[i];
    }
    int maxlen = -1;
    int minlen = INF;
    for (int i = 0; i < M; ++i) {  // 不能有新的木棍为空。
      if (!len[i]) {
        return;
      }
      if (len[i] > maxlen) {
        maxlen = len[i];
      }
      if (len[i] < minlen) {
        minlen = len[i];
      }
    }

    int diff = maxlen - minlen;
    // printf("max %d min %d diff %d\n", maxlen, minlen, diff);

    if (diff < min_diff) {
      min_diff = diff;
    }

    return;
  }

  for (int i = 0; i < M; ++i) {
    Mask[cur] = i;
    Solve(cur + 1);
  }
}

void Read() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &Len[i]);
  }
}

int main() {
  freopen("in2.txt", "r", stdin);

  Read();
  Solve(0);
  printf("%d\n", min_diff);

  return 0;
}